<?php
/**
 * 用户操作类
 * Created by PhpStorm.
 * User: jason
 * Date: 18-8-22
 * Time: 上午9:36
 */
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Validator;
use Cookie;
use Illuminate\Support\Facades\DB;
use View;
class UserController extends Controller
{
    //用户登录
    public function login(Request $request){
        if ($request->isMethod('post')) {
            $rule = [
                'mobile' => 'required|regex:/^1[345789][0-9]{9}$/',
                'password' => 'required',
            ];
            $message = [
                'mobile.required' => 'Please enter mobile number',
                'mobile.regex' => 'Wrong phone number format',
                'password.required' => 'Please input a password',
            ];
            $validator = Validator::make($request->all(), $rule, $message);
            if ($validator->fails()) {
                $errors = $validator->errors();
                return view('user.login',['error' => self::getValidatorError($errors)]);
            }
            $where = [
                ['mobile', $request->input('mobile')]
            ];
            $member = DB::table('member')->where($where)->first();
            if (!$member) {
                return view('user.login',['error'=>'The user does not exist']);
            }
            if (password_verify($request->input('password'),$member->password) === false) {
                return view('user.login',['error'=>'The password is wrong']);
            }
            DB::table('member')->where('member_id', $member->member_id)->update(['login_time' => time()]);
            Cookie::queue('laravel_cookie', $member->member_id, 10);
            $request->member_info = $member;
            $request->member_id = $member->member_id;
            return redirect('/home');
        }
        return view('user.login');
    }

    //用户注册
    public function register(Request $request){
        if ($request->isMethod('post')) {
            $rule = [
                'mobile' => 'required|numeric|regex:/^1[345789][0-9]{9}$/',
                'password' => 'required',
                'email' => 'required|email',
            ];
            $message = [
                'mobile.required' => 'Please enter mobile number',
                'mobile.regex' => 'Wrong phone number format',
                'password.required' => 'Please input a password',
                'email.required' => 'Please input a email',
                'email.email' => 'Wrong email format',
            ];
            $validator = Validator::make($request->all(),$rule,$message);
            if ($validator->fails()) {
                $error = $validator->errors();
                return view('user.register',['error'=>self::getValidatorError($error)]);
            }
            $isMobile = DB::table('member')->where('mobile', $request->input('mobile'))->value('member_id');
            if ($isMobile) {
                $data = [
                    'code' => 200,
                    'msg' => 'The mobile phone number has been registered.',
                ];
                return $data;
            }
            $isEmail = DB::table('member')->where('email', $request->input('email'))->value('member_id');
            if ($isEmail) {
                $data = [
                    'code' => 200,
                    'msg' => 'Mail has been registered',
                ];
                return $data;
            }
            $password = $request->input('password');
            //哈希加密 设置cost
            $password = password_hash($password , PASSWORD_DEFAULT , ['cost' => 8]);
            if (!$password) {
                $data = [
                    'code' => 200,
                    'msg' => 'password_hash is fails',
                ];
                return $data;
            }
            $data = [
                'mobile' => $request->input('mobile'),
                'password' => $password,
                'email' => $request->input('email'),
                'status' => 1,
                'addtime' => time(),
            ];
            $member_id = DB::table('member')->insert($data);
            if (!$member_id) {
                $data = [
                    'code' => 200,
                    'msg' => 'register is fails',
                ];
                return $data;
            }
            return redirect('/login');
        }
        return view('user.register');
    }

    //登出
    public function login_out(Request $request){
        if ($request->hasCookie('laravel_cookie')) {
            Cookie::queue(Cookie::forget('laravel_cookie'));
        }
        echo "<script>alert('LOGIN OUT SUCCESS')</script>";
        return redirect('/');
    }
}